// pages/home/components/qualifications_1/qualifications_1.js
const http = require('../../../../http/app.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    last_question: '',
    last_answer: '',
    infoList: [],
    infoIndex: -1,
    problem: '',
    infoMess: {},
    showtips: []
  },
  infoList(e) { //点击选项
    const that =this
    const ind = e.currentTarget.dataset.id;
    const problem_id = this.data.infoList[ind].option_abid;
    const answer_id = this.data.infoList[ind].option_id;
    const last_answer = this.data.infoList[ind].option_content;
    const last_question = this.data.problem;
    this.data.infoMess.question.forEach(function (item, index, arr) { // 过滤
      if (item == problem_id) {
        arr.splice(index);
        that.data.infoMess.answer.splice(index);
      }
    })
    this.setData({
      infoIndex: ind
    })
    if (problem_id) {
      const question = this.data.infoMess.question.concat(problem_id);
      const answer = this.data.infoMess.answer.concat(answer_id);
      this.data.infoMess.last_question = last_question;
      this.data.infoMess.last_answer = last_answer;
      this.data.infoMess.question = question;
      this.data.infoMess.answer = answer;
      wx.setStorageSync('infoMessage', this.data.infoMess)// 存储本地
      // 把值传给后台
      const that = this;
      const infoList = {
        question: question,
        answer: answer
      }
      console.log(infoList)
      wx.request({// 获取页面数据
        url: http.selAnswer,
        method: 'POST',
        data: infoList,
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success(res) {
          // console.log(res)
          const sortnum = res.data.sortnum
          const answers = JSON.stringify(res.data.answerre.answer_content)
          if (res.data.re == 'succ') {
            wx.navigateTo({//跳转下一个页面
              url: '../qualifications_1/qualifications_1?sortnum=' + sortnum,
            })
          } else {
            wx.navigateTo({//跳转结果页面
              url: '../qualifications_2/qualifications_2?answers=' + answers,
            })
          }
        }
      })
    }
  
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '购房资质'
    })
    wx.showLoading({ // 加载数据前的loading
      title: '数据加载中...'
    })
    const sortnum = options.sortnum;
    const that = this;
    wx.request({// 获取页面数据
      url: http.abilityOne,
      method: 'POST',
      data: { sortnum: sortnum },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        // console.log(res)
        wx.hideLoading() // 数据调出后关闭loading
        const result = res.data.optionre
        const problem = res.data.showre.ability_content
        const showtips = res.data.showre.showtips
        that.setData({
          infoList: result,
          problem: problem,
          showtips: showtips
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this
    wx.getStorage({
      key: 'infoMessage',
      success(res) {
        // console.log(res.data)
        that.setData({
          infoMess: res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})
